#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <android/bitmap.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "mybitmap.h"
#define  LOG_TAG    "createbitmap"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif

Bitmap* bitmap = NULL;
Bitmap* createBitmap(AndroidBitmapInfo*  info, void*  pixels);
void destroyBitmap(Bitmap* bitmap);

JNIEXPORT void JNICALL Java_com_npsngs_hawkeye_MainActivity_nativeSetBitMap(JNIEnv * env, jclass cls, jobject bitmap)
{
    AndroidBitmapInfo  info;
    void*              pixels;
    int                ret;
    static int         init;

    if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }


    if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }
   //now create bitmap and copy pixels

	Bitmap* mBitmap = createBitmap(&info, pixels);
	if(NULL != mBitmap)
	{
		HistoryNode* node = (HistoryNode*)malloc(sizeof(HistoryNode));
		node->bitmap = mBitmap;
		node->op_type = OP_TYPE_NONE;
		if(NULL != historyStack) emptyHistory();
		historyStack = (HistoryStack*)malloc(sizeof(HistoryStack));
		historyStack->depth = 0;
		pushHistory(node);	
	}	

}

void destroyBitmap(Bitmap* bitmap)
{	
	if(bitmap && bitmap->pixels){
		free(bitmap->pixels);
		free(bitmap);
	}
}
int unpack_argb_565(unsigned char* pixel,unsigned char* a,
unsigned char* r, unsigned char* g, unsigned char* b)
{
        unsigned short color = *(unsigned short*)pixel;
	*a = 0xff;
        *r = ((color >> 11) & 0xff) << 3;
        *g = ((color >> 5) & 0xff)  << 2;
        *b = (color & 0xff )<< 3;

        return 2;
}

int unpack_argb_4444(unsigned char* pixel,unsigned char* a,
        unsigned char* r, unsigned char* g, unsigned char* b)
{
	unsigned short color = *(unsigned short*)pixel;
	*a = ((color >> 12) & 0xff) >> 4;
        *r = ((color >> 8) & 0xff) >> 4;
        *g = ((color >> 4) & 0xff) >> 4;
        *b = ( color & 0xff) >> 4;
        return 3;
}
int unpack_argb_8888(unsigned char* pixel,unsigned char* a,
        unsigned char* r, unsigned char* g, unsigned char* b)
{
	*a = pixel[0];
        *r = pixel[1];
        *g = pixel[2];
        *b = pixel[3];
        return 4;
}

int unpack_a_8(unsigned char* pixel,unsigned char* a,
        unsigned char* r, unsigned char* g, unsigned char* b)
{
	*a = 0xff;
        *r = pixel[0];
        *g = pixel[0];
        *b = pixel[0];
        return 1;
}

int unpack_none(unsigned char* pixel,unsigned char* a,
        unsigned char* r, unsigned char* g, unsigned char* b)
{
        *r = *g = *b = 0;
        return -1;
}


Bitmap* createBitmap(AndroidBitmapInfo*  info, void*  pixels)
{
	bitmap  = (Bitmap*)malloc(sizeof(Bitmap));
	if(NULL == bitmap)return NULL;
	bitmap->width = info->width;
	bitmap->height = info->height;
	bitmap->format = info->format;
	LOGI("w = %d h=%d format= %d",bitmap->width,bitmap->height,bitmap->format);
	size_t pixelNum = bitmap->height * bitmap->width;
	size_t length = pixelNum * 32;
	switch(bitmap->format)
	{
		case BITMAP_FORMAT_RGBA_8888:
			bitmap->unpack = unpack_argb_8888;
			length = pixelNum * 4;
			break;
		case BITMAP_FORMAT_RGB_565:
			bitmap->unpack = unpack_argb_565;
			length = pixelNum * 2;
			break;
		case BITMAP_FORMAT_RGBA_4444:
			bitmap->unpack = unpack_argb_4444;
			length = pixelNum * 2;
			break;
		case BITMAP_FORMAT_A_8:
			bitmap->unpack = unpack_a_8;
			length = pixelNum;
			break;
		default:
			bitmap->unpack = unpack_none;
			break;
	}
	bitmap->pixels = malloc(length);
	LOGI("pixels length: %d",length);
	memcpy(bitmap->pixels, pixels, length);
	return bitmap;
}
#ifdef __cplusplus
}
#endif

